// pages/mine/address/address.js
const app = getApp();
const util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addList: [],
    productId: "",
    sizeId: ""
  },

  // 点击添加地址
  toAddAddress: function(e) {
    // 判断是否绑定手机号
    let phone = wx.getStorageSync('phone')
    if (phone.length == 11) {
      wx.navigateTo({
        url: 'addAddress/addAddress',
      })
    } else {
      wx.showModal({
        title: '请先绑定手机号',
        showCancel: false
      })
    }
  },

  // 点击删除地址
  deteleAdd: function(e) {
    let that = this
    wx.showModal({
      title: '确认删除',
      content: '您确认删除该地址吗',
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        // console.log(res);
        if (res.confirm) {
          // 发送删除地址请求
          let delUrl = app.globalData.baseUrl + "weixin/deleteAddress"
          let data = {
            addressId: e.target.id
          }
          util.requestLoading(delUrl, data, "GET").then(res => {
            // console.log(res)
            // 请求收货地址列表
            let addressUrl = app.globalData.baseUrl + "weixin/addressList"
            let data = {
              cid: wx.getStorageSync('cid')
            }
            util.requestLoading(addressUrl, data, "GET").then(res => {
              // console.log(res)
              that.setData({
                addList: res.data.addressList,
                next: 0
              })
            })
          })
        }
      }
    })
  },

  // 点击编辑地址
  compileAdd: function (e) {
    let that = this
    wx.navigateTo({
      url: 'compileAdd/compileAdd?addressId='+e.target.id
    })
  },

  // 点击选择收货地址
  next: function(e) {
    let that = this
    // console.log(e)
    // console.log(e.currentTarget.id, that.data.productId, that.data.sizeId)
    if (that.data.addressId != undefined && that.data.productId != undefined && that.data.sizeId!=undefined){
      wx.navigateTo({
        url: '../myform/confirmOrder/confirmOrder?addressId=' + that.data.addressId + '&productId=' + that.data.productId + '&sizeId=' + that.data.sizeId
      })
    }else{
      let addressUrl = app.globalData.baseUrl + "weixin/changeAddress"
      let data = {
        orderId: that.data.orderId,
        addressId: that.data.addressId
      }
      util.requestLoading(addressUrl, data, "GET").then(res => {
        console.log(res)
        wx.navigateTo({
          url: '../myform/payDetails/payDetails?orderId=' + that.data.orderId
        })
      })
    }
  },
//选中地址栏
  selectedItem: function(res){
     console.log(res.currentTarget.dataset.id)
    var addList = this.data.addList
    for (var i = 0; i < addList.length;i++){
      addList[i].selected = false
    }
    addList[res.currentTarget.dataset.id].selected = true

    this.setData({
      addList: addList,
      next: 1,
      addressId: addList[res.currentTarget.dataset.id].addressId
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    that.setData({
      productId: options.productId,
      sizeId: options.sizeId,
      orderId: options.orderId
    })
    // console.log(that.data.productId)
    // 请求收货地址列表
    let addressUrl = app.globalData.baseUrl + "weixin/addressList"
    let data = {
      cid : wx.getStorageSync('cid')
    }
    util.requestLoading(addressUrl, data, "GET").then(res => {
      // console.log(res)
      this.setData({
        addList: res.data.addressList,
        next:0
      })
    })
  },

  onShow:function(e) {
    let that = this 
    // 请求收货地址列表
    let addressUrl = app.globalData.baseUrl + "weixin/addressList"
    let data = {
      cid: wx.getStorageSync('cid')
    }
    util.requestLoading(addressUrl, data, "GET").then(res => {
      // console.log(res)
      this.setData({
        addList: res.data.addressList,
        next:0
      })
    })
  },
})